package cscie160.htest.atm;
/**
 * this calss will be talking to BankServer
*/

import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

public class ATMImpl extends UnicastRemoteObject implements ATM {

    private byte byteField;
    private cscie160.htest.bank.BankServer bServer;
  
    public ATMImpl() throws RemoteException {
        this((byte)0);
    }
 
    public ATMImpl(byte byteField) throws RemoteException {
        super();
        this.byteField = byteField;
  
        int bankServerPort = 50001;
        String serverLookupUrl = "//localhost:" + Integer.toString(bankServerPort) + "/bankserver";
        try{
            bServer = (cscie160.htest.bank.BankServer)Naming.lookup(
serverLookupUrl);
        } catch (java.rmi.NotBoundException nbex) {
            System.out.println(nbex.getClass().getName() + "; message=" + nbex.getMessage());
        } catch (java.net.MalformedURLException mue) {
            System.out.println(mue.getClass().getName() + "; message=" + mue.getMessage());
        }
    }

    public byte getByteField() throws RemoteException {
        return bServer.getByteField();
    }

    public void setByteField(byte byteField) throws RemoteException {
        this.bServer.setByteField(byteField);
    }

    public static void main(String args[])
{

        //Create and install a security manager

        System.setSecurityManager(new RMISecurityManager());

        try
 {

            int port = 1099;

            if (args.length > 0 && !(args[0].equals("?") || args[0].equalsIgnoreCase("help"))){
                try{
                    port = Integer.parseInt(args[0]);
                } catch (NumberFormatException nfex){
                    System.out.println("Provided argument\"" + args[0] + "\" can't be a port");
                    return;
                }
            }
            java.rmi.registry.Registry registry =
 
                java.rmi.registry.LocateRegistry.createRegistry(port);

            //ATMFactoryImpl anAtmFact = new ATMFactoryImpl();

            //Naming.rebind("//localhost:" + Integer.toString(port) + "/atmfactory", anAtmFact);


            ATMImpl anAtm = new ATMImpl();

            Naming.rebind("//localhost:" + Integer.toString(port) + "/atm", anAtm );
            System.out.println("atm bound in registry on port " + Integer.toString(port));
            
        } catch (Exception e)
 {

            System.out.println("Server err: " + e.getClass().getName() + ". Message="+e.getMessage());

            e.printStackTrace();

        }

        System.out.println("atm is exiting its \"main()\" method");
    }
}
